package persistence;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import domain.Class;
import domain.Professor;


public class ProfessorHandler {
	
	private final String SEPARATOR = ", ";
	private final String EMPTY_STRING = "";

	Statement stmt;	

	public ProfessorHandler() throws SQLException {
		Connection conn;
		try {
			conn = DBConnection.getInstance().getDb_conn();
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public boolean create(Professor professor) throws SQLException {

		String sqlComand = "INSERT INTO Professores VALUES (";

		try {

			sqlComand += professor.getName()
					+ SEPARATOR
					+ professor.getRegistration()
					+ ");";			
			
			stmt.execute(sqlComand);

			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}

	public boolean delete(String registration) {

		String sqlComand = "DELETE FROM Professores WHERE ";

		try {
			sqlComand += "registration = " + registration + ";";
			
			stmt.execute(sqlComand);

			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}

	public boolean update(String name, String registration) {

		String sqlComand = "UPDATE Professores SET ";

		try {
			sqlComand += "name = " + name ;
			
			sqlComand += "WHERE registration = " + registration + ";";
			
			stmt.execute(sqlComand);

			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}

	public Professor retreive(String registration) {

		ResultSet stmtResult;
		Professor result;

		String sqlComand = "SELECT name FROM Professores WHERE ";

		try {
			
			sqlComand += "registration = " + registration + ";";
			stmtResult = stmt.executeQuery(sqlComand);
			
			result = new Professor(stmtResult.getString(1), registration);
			
			return result;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
